package com.java.model.proxy;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.context.support.GenericApplicationContext;

public class TestProxyFactory2 {
    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("aspect1", Aspect1.class);
        context.registerBean("aspect2", Aspect2.class);
        context.registerBean(AnnotationAwareAspectJAutoProxyCreator.class);// 自动代理后处理器
        context.registerBean("target1", Target1.class);
        context.refresh();
        Target1 target1 = context.getBean(Target1.class);
        target1.foo();
    }


    static class Target1 {
        public void foo() {
            System.out.println("foo");
        }
    }

    @Aspect
    static class Aspect1 {
        @Around("execution(* foo())")
        public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
            System.out.println("aspect1 around");
            return joinPoint.proceed();

        }
    }

    @Aspect
    static class Aspect2 {
        @Before("execution(* foo())")
        public void before() {
            System.out.println("aspect2 before");

        }

        @After("execution(* foo())")
        public void after() {
            System.out.println("aspect2 after");

        }
    }
}
